home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / ASTRONOM / H139.ZIP / UI101.ZIP / IO / GR / GR_DEF.C next >
C/C++ Source or Header  |  1991-11-04  |  13KB  |  480 lines

  1. /****************************************************************
  2.  
  3.     gr_def.c        default line, rectangle, and circle
  4.             routines for Bywater
  5.             Graphics Interface standard
  6.  
  7.             Copyright (c) 1991, Ted A. Campbell
  8.  
  9.             Bywater Software
  10.             P. O. Box 4023
  11.             Duke Station
  12.             Durham, NC  27706
  13.  
  14.             email: tcamp@hercules.acpub.duke.edu
  15.  
  16.     Copyright and Permissions Information:
  17.  
  18.     All U.S. and international copyrights are claimed by the
  19.     author. The author grants permission to use this code
  20.     and software based on it under the following conditions:
  21.     (a) in general, the code and software based upon it may be
  22.     used by individuals and by non-profit organizations; (b) it
  23.     may also be utilized by governmental agencies in any country,
  24.     with the exception of military agencies; (c) the code and/or
  25.     software based upon it may not be sold for a profit without
  26.     an explicit and specific permission from the author, except
  27.     that a minimal fee may be charged for media on which it is
  28.     copied, and for copying and handling; (d) the code must be
  29.     distributed in the form in which it has been released by the
  30.     author; and (e) the code and software based upon it may not
  31.     be used for illegal activities.
  32.  
  33. ****************************************************************/
  34.  
  35.  
  36. #include "stdio.h"
  37. #include "math.h"
  38.  
  39. #ifdef __STDC__
  40. #include "malloc.h"
  41. #else
  42. extern char * malloc();
  43. #define size_t  int
  44. #endif
  45.  
  46. #include "bw.h"
  47. #include "gr.h"
  48. #include "kb.h"
  49.  
  50. #define CHECK_PARAMS    FALSE
  51. #ifndef DEG_RAD
  52. #define RAD_DEG         5.729577951e1   /* Convert radians to degrees   */
  53. #define DEG_RAD         1.745329252e-2  /* Convert degrees to radians   */
  54. #endif
  55. #ifndef ACCURACY
  56. #define ACCURACY        10
  57. #endif
  58.  
  59. #ifdef  STANDALONE
  60. struct gr_window main_window;
  61. char tbuf[ 128 ];
  62. char bw_ebuf[ BW_EBUFSIZE ];
  63. int x_pos, y_pos, b_stat;
  64.  
  65. main()
  66.    {
  67.    register int c;
  68.    int x, y;
  69.  
  70.    gr_init( &main_window, NULL );
  71.    kb_init();
  72.  
  73.    x = main_window.xmax / 8;
  74.    y = ( main_window.ymax / 4 ) * 3;
  75.    c = 0;
  76.    while ( ( c < gr_colors ) && ( y > main_window.fysize ) )
  77.       {
  78.       def_line( GR_PRIMARY, x, y, x + main_window.xmax / 20, y,
  79.      c, SOLID );
  80.       x += main_window.xmax / 10;
  81.       def_line( GR_PRIMARY, x, y, x + main_window.xmax / 20, y,
  82.      c, HATCH );
  83.       sprintf( tbuf, " <- lines, color %d at x = %d, y = %d",
  84.      c, x, y );
  85.       gr_text( GR_PRIMARY, x + ( 3 * ( main_window.xmax / 20 )),
  86.      y - ( main_window.fysize / 2 ), tbuf, WHITE, BLACK );
  87.       ++c;
  88.       x -= main_window.xmax / 10;
  89.       y -= ( main_window.fysize / 2 ) * 3;
  90.       }
  91.    kb_rx();
  92.  
  93.    gr_cls( GR_PRIMARY );
  94.    def_line( GR_PRIMARY, main_window.xmax / 2, main_window.ymax,
  95.       main_window.xmax, main_window.ymax / 2, WHITE, GRID );
  96.    def_line( GR_PRIMARY, main_window.xmax, main_window.ymax / 2,
  97.       main_window.xmax / 2, 0, WHITE, GRID );
  98.    def_line( GR_PRIMARY, main_window.xmax / 2, 0,
  99.       0, main_window.ymax / 2, WHITE, GRID );
  100.    def_line( GR_PRIMARY, 0, main_window.ymax / 2,
  101.       main_window.xmax / 2, main_window.ymax, WHITE, GRID );
  102.    def_line( GR_PRIMARY, 0, main_window.ymax / 2,
  103.       main_window.xmax, main_window.ymax / 2, WHITE, SOLID );
  104.    def_line( GR_PRIMARY, main_window.xmax / 2, 0,
  105.       main_window.xmax / 2, main_window.ymax, WHITE, SOLID );
  106.    kb_rx();
  107.  
  108.    gr_cls( GR_PRIMARY );
  109.    x = main_window.xmax / 40;
  110.    y = main_window.ymax / 20;
  111.    def_rectangle( GR_PRIMARY, x * 4, y * 16, x * 6, y * 18, WHITE, HOLLOW );
  112.    gr_text( GR_PRIMARY, x * 10, y * 17, "Hollow rectangle", WHITE, BLACK );
  113.    def_rectangle( GR_PRIMARY, x * 4, y * 12, x * 6, y * 14, WHITE, SOLID );
  114.    gr_text( GR_PRIMARY, x * 10, y * 13, "Solid rectangle", WHITE, BLACK );
  115.     def_rectangle( GR_PRIMARY, x * 4, y * 8, x * 6, y * 10, WHITE, GRID );
  116.    gr_text( GR_PRIMARY, x * 10, y * 9, "Grid rectangle", WHITE, BLACK );
  117.     def_rectangle( GR_PRIMARY, x * 4, y * 4, x * 6, y * 6, WHITE, HATCH );
  118.    gr_text( GR_PRIMARY, x * 10, y * 5, "Hatch rectangle", WHITE, BLACK );
  119.    kb_rx();
  120.  
  121.    gr_cls( GR_PRIMARY );
  122.    x = main_window.xmax / 40;
  123.    y = main_window.ymax / 20;
  124.    def_circle( GR_PRIMARY, x * 5, y * 17, (y/2)*3, WHITE, HOLLOW );
  125.    gr_text( GR_PRIMARY, x * 10, y * 17, "Hollow circle", WHITE, BLACK );
  126.    def_circle( GR_PRIMARY, x * 5, y * 13, (y/2)*3, WHITE, SOLID );
  127.    gr_text( GR_PRIMARY, x * 10, y * 13, "Solid circle", WHITE, BLACK );
  128.     def_circle( GR_PRIMARY, x * 5, y * 9, (y/2)*3, WHITE, GRID );
  129.    gr_text( GR_PRIMARY, x * 10, y * 9, "Grid circle", WHITE, BLACK );
  130.     def_circle( GR_PRIMARY, x * 5, y * 5, (y/2)*3, WHITE, HATCH );
  131.    gr_text( GR_PRIMARY, x * 10, y * 5, "Hatch circle", WHITE, BLACK );
  132.  
  133.    kb_rx();
  134.    kb_deinit();
  135.    gr_deinit();
  136.    }
  137.  
  138. #endif
  139.  
  140. /****************************************************************
  141.  
  142.     def_line()
  143.  
  144. ****************************************************************/
  145.  
  146. def_line( screen, x1, y1, x2, y2, color, style )
  147.    int screen;
  148.    int x1, y1, x2, y2;
  149.    int color, style;
  150.    {
  151.    register int t, dist;
  152.    int xerr, yerr, dx, dy, incx, incy;
  153.    int x_val, y_val;
  154.  
  155. #if CHECK_PARAMS
  156.    if ( ( x1 < 0 ) || ( x1 > main_window.xmax ))
  157.       {
  158.       sprintf( bw_ebuf, "[pr:] gr_line(): x1 value is %d", x1 );
  159.       bw_error( bw_ebuf );
  160.       return BW_ERROR;
  161.       }
  162.    if ( ( x2 < 0 ) || ( x2 > main_window.xmax ))
  163.       {
  164.       sprintf( bw_ebuf, "[pr:] gr_line(): x2 value is %d", x2 );
  165.       bw_error( bw_ebuf );
  166.       return BW_ERROR;
  167.       }
  168.    if ( ( y1 < 0 ) || ( y1 > main_window.ymax ))
  169.       {
  170.       sprintf( bw_ebuf, "[pr:] gr_line(): y1 value is %d", y1 );
  171.       bw_error( bw_ebuf );
  172.       return BW_ERROR;
  173.       }
  174.    if ( ( y2 < 0 ) || ( y2 > main_window.ymax ))
  175.       {
  176.       sprintf( bw_ebuf, "[pr:] gr_line(): y2 value is %d", y2 );
  177.       bw_error( bw_ebuf );
  178.       return BW_ERROR;
  179.       }
  180. #endif
  181.  
  182.    xerr = yerr = 0;
  183.    dx = x2 - x1;
  184.    dy = y2 - y1;
  185.    x_val = x1;
  186.    y_val = y1;
  187.  
  188.    if ( dx > 0 )
  189.       {
  190.       incx = 1;
  191.       }
  192.    else if ( dx == 0 )
  193.       {
  194.       incx = 0;
  195.       }
  196.    else
  197.       {
  198.       incx = -1;
  199.       }
  200.  
  201.    if ( dy > 0 )
  202.       {
  203.       incy = 1;
  204.       }
  205.    else if ( dy == 0 )
  206.       {
  207.       incy = 0;
  208.       }
  209.    else
  210.       {
  211.       incy = -1;
  212.       }
  213.  
  214.    dx = abs(dx);
  215.    dy = abs(dy);
  216.  
  217.    dist = (dx > dy) ? dx : dy;
  218.  
  219.    for ( t = 0; t <= ( dist + 1 ); t++ )
  220.       {
  221.       switch( style )
  222.      {
  223.      case HOLLOW:
  224.         gr_pixel( screen, x_val, y_val, BLACK );
  225.         break;
  226.      case SOLID:
  227.         gr_pixel( screen, x_val, y_val, color );
  228.         break;
  229.      case GRID:
  230.      case HATCH:
  231.         if ( ( ( x_val + y_val ) % 2 ) == 1 )
  232.            {
  233.            gr_pixel( screen, x_val, y_val, color );
  234.            }
  235.         break;
  236.      }
  237.       xerr += dx;
  238.       yerr += dy;
  239.       if ( xerr > dist )
  240.      {
  241.      xerr -= dist;
  242.      x_val += incx;
  243.      }
  244.       if ( yerr > dist )
  245.      {
  246.      yerr -= dist;
  247.      y_val += incy;
  248.      }
  249.       }
  250.    }
  251.  
  252. /****************************************************************
  253.  
  254.     def_rectangle()
  255.  
  256. ****************************************************************/
  257.  
  258. def_rectangle( screen, x1, y1, x2, y2, color, style )
  259.    int screen;
  260.    int x1, y1, x2, y2;
  261.    int color, style;
  262.    {
  263.    register int x_val, y_val;
  264.  
  265. #if CHECK_PARAMS
  266.    if ( ( x1 < 0 ) || ( x1 > main_window.xmax ))
  267.       {
  268.       sprintf( bw_ebuf, "[pr:] gr_rectangle(): x1 value is %d", x1 );
  269.       bw_error( bw_ebuf );
  270.       return BW_ERROR;
  271.       }
  272.    if ( ( x2 < 0 ) || ( x2 > main_window.xmax ))
  273.       {
  274.       sprintf( bw_ebuf, "[pr:] gr_rectangle(): x2 value is %d", x2 );
  275.       bw_error( bw_ebuf );
  276.       return BW_ERROR;
  277.       }
  278.    if ( ( y1 < 0 ) || ( y1 > main_window.ymax ))
  279.       {
  280.       sprintf( bw_ebuf, "[pr:] gr_rectangle(): y1 value is %d", y1 );
  281.       bw_error( bw_ebuf );
  282.       return BW_ERROR;
  283.       }
  284.    if ( ( y2 < 0 ) || ( y2 > main_window.ymax ))
  285.       {
  286.       sprintf( bw_ebuf, "[pr:] gr_rectangle(): y2 value is %d", y2 );
  287.       bw_error( bw_ebuf );
  288.       return BW_ERROR;
  289.       }
  290. #endif
  291.  
  292.    /*  First draw border around the rectangle */
  293.  
  294.    gr_line( screen, x1, y1, x2, y1, color, SOLID );
  295.    gr_line( screen, x2, y1, x2, y2, color, SOLID );
  296.    gr_line( screen, x1, y1, x1, y2, color, SOLID );
  297.    gr_line( screen, x1, y2, x2, y2, color, SOLID );
  298.  
  299.    switch( style )
  300.       {
  301.       case HOLLOW:
  302.      break;
  303.       case SOLID:
  304.      for ( y_val = y1 + 1; y_val < y2; ++y_val )
  305.         {
  306.         gr_line( screen, x1 + 1, y_val, x2 - 1, y_val, color, SOLID );
  307.         }
  308.      break;
  309.       case GRID:
  310.      for ( y_val = y1 + 1; y_val < y2; ++y_val )
  311.         {
  312.         gr_line( screen, x1 + 1, y_val, x2 - 1, y_val, color, GRID );
  313.         }
  314.      break;
  315.       case HATCH:
  316.      for ( y_val = y1 + 1; y_val < y2; ++y_val )
  317.         {
  318.         if ( ( y_val % 3 ) == 1 )
  319.            {
  320.            gr_line( screen, x1 + 1, y_val, x2 - 1, y_val, color, SOLID );
  321.            }
  322.         }
  323.      for ( x_val = x1 + 1; x_val < x2; ++x_val )
  324.         {
  325.         if ( ( x_val % 3 ) == 1 )
  326.            {
  327.            gr_line( screen, x_val, y1 + 1, x_val, y2 - 1, color, SOLID );
  328.            }
  329.         }
  330.      break;
  331.       }
  332.  
  333.    }
  334.  
  335. /****************************************************************
  336.  
  337.    def_circle()    Draw circle using gr line routines
  338.  
  339. ****************************************************************/
  340.  
  341. def_circle( screen, x, y, radius, color, style )
  342.    int screen;
  343.    int x, y, radius;
  344.    int color, style;
  345.    {
  346.    register int y_val;
  347.    int top_y, bot_y, prev_ty, prev_by;
  348.    int x1, x2, prev_x1, prev_x2;
  349.    int width, x_val;
  350.    double angle, y_cor;
  351.  
  352. #ifdef  OLD_DEBUG
  353.    sprintf( bw_ebuf, "circle: r %d, y %d, x %d   ",
  354.       radius, y, x );
  355.    bw_error( bw_ebuf );
  356. #endif
  357. #if CHECK_PARAMS
  358.    if ( ( x < 0 ) || ( x > main_window.xmax ))
  359.       {
  360.       sprintf( bw_ebuf, "[pr:] def_circle(): x value is %d", x );
  361.       bw_error( bw_ebuf );
  362.       return BW_ERROR;
  363.       }
  364.    if ( ( y < 0 ) || ( y > main_window.ymax ))
  365.       {
  366.       sprintf( bw_ebuf, "[pr:] def_circle(): y value is %d", y );
  367.       bw_error( bw_ebuf );
  368.       return BW_ERROR;
  369.       }
  370. #endif
  371.  
  372.    width = ( radius * gr_pysize * ACCURACY ) / ( gr_pxsize * ACCURACY );
  373.    prev_by = prev_ty = bot_y = top_y = y;
  374.    prev_x1 = x1 = x - width;
  375.    prev_x2 = x2 = x + width;
  376.  
  377.    switch( style )
  378.       {
  379.       case HOLLOW:
  380.      break;
  381.       case SOLID:
  382.      gr_line( screen, x1 + 1, top_y, x2 - 1, top_y, color, SOLID );
  383.      gr_line( screen, x1 + 1, bot_y, x2 - 1, bot_y, color, SOLID );
  384.      break;
  385.       case GRID:
  386.      gr_line( screen, x1 + 1, top_y, x2 - 1, top_y, color, GRID );
  387.      gr_line( screen, x1 + 1, bot_y, x2 - 1, bot_y, color, GRID );
  388.      break;
  389.       case HATCH:
  390.      if ( ( top_y % 2 ) == 1 )
  391.         {
  392.         gr_line( screen, x1 + 1, top_y, x2 - 1, top_y, color, SOLID );
  393.         gr_line( screen, x1 + 1, bot_y, x2 - 1, bot_y, color, SOLID );
  394.         }
  395.      break;
  396.       }
  397.    for ( y_val = 1; y_val <= ( radius - 1 ); ++y_val )
  398.       {
  399.       top_y = y + y_val;
  400.       bot_y = y - y_val;
  401.  
  402.       y_cor = ( y_val * gr_pysize * ACCURACY ) / ( gr_pxsize * ACCURACY );
  403.       angle = asin( ( y_cor ) / ((double) width ) );
  404.  
  405. #ifdef  OLD_DEBUG
  406.       sprintf( bw_ebuf, "angle %.2lf deg   ",
  407.      angle * RAD_DEG );
  408.       bw_error( bw_ebuf );
  409. #endif
  410.  
  411. #ifdef  OLD_DEBUG
  412.       sprintf( bw_ebuf, "tan( angle ) %.2lf deg   ",
  413.      tan( angle ) );
  414.       bw_error( bw_ebuf );
  415. #endif
  416.       x_val = (int) ( y_cor / tan( angle ));
  417. #ifdef  OLD_DEBUG
  418.       sprintf( bw_ebuf, "angle %.2lf deg, y %d, x %d   ",
  419.      angle * RAD_DEG, y_val, x_val );
  420.       bw_error( bw_ebuf );
  421. #endif
  422.  
  423.       x1 = x - x_val;
  424.       x2 = x + x_val;
  425.  
  426.       gr_line( screen, x2, top_y, prev_x2, prev_ty, color, SOLID );
  427.       gr_line( screen, x1, top_y, prev_x1, prev_ty, color, SOLID );
  428.       gr_line( screen, x2, bot_y, prev_x2, prev_by, color, SOLID );
  429.       gr_line( screen, x1, bot_y, prev_x1, prev_by, color, SOLID );
  430.  
  431.       switch( style )
  432.      {
  433.      case HOLLOW:
  434.         break;
  435.      case SOLID:
  436.         gr_line( screen, x1 + 1, top_y, x2 - 1, top_y, color, SOLID );
  437.         gr_line( screen, x1 + 1, bot_y, x2 - 1, bot_y, color, SOLID );
  438.         break;
  439.      case GRID:
  440.         gr_line( screen, x1 + 1, top_y, x2 - 1, top_y, color, GRID );
  441.         gr_line( screen, x1 + 1, bot_y, x2 - 1, bot_y, color, GRID );
  442.         break;
  443.      case HATCH:
  444.         if ( ( top_y % 2 ) == 1 )
  445.            {
  446.            gr_line( screen, x1 + 1, top_y, x2 - 1, top_y, color, SOLID );
  447.            gr_line( screen, x1 + 1, bot_y, x2 - 1, bot_y, color, SOLID );
  448.            }
  449.         break;
  450.      }
  451.  
  452.       prev_x1 = x1;
  453.       prev_x2 = x2;
  454.       prev_ty = top_y;
  455.       prev_by = bot_y;
  456.  
  457.       }
  458.  
  459.    x2 = x1 = x;
  460.    top_y = y + radius;
  461.    bot_y = y - radius;
  462.  
  463.    gr_line( screen, x2, top_y, prev_x2, prev_ty, color, SOLID );
  464.    gr_line( screen, x1, top_y, prev_x1, prev_ty, color, SOLID );
  465.    gr_line( screen, x2, bot_y, prev_x2, prev_by, color, SOLID );
  466.    gr_line( screen, x1, bot_y, prev_x1, prev_by, color, SOLID );
  467.    }
  468.  
  469. #ifdef  STANDALONE
  470.  
  471. bw_error( s )
  472.    char *s;
  473.    {
  474.    gr_text( GR_PRIMARY, 10, 10, s, WHITE, BLACK );
  475.    kb_rx();
  476.    }
  477.  
  478. #endif
  479.  
  480.